package com.kk.app;

import android.app.Presentation;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;


import com.kk.app.databinding.PlayerPresentationLayoutBinding;
import com.kk.app.render.ISurfaceRenderListener;
import com.kk.app.render.MVVideoRender;
import com.kk.app.render.RenderViewBase;

import java.lang.ref.WeakReference;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


/**
 * 显示
 */
class PlayerPresentation extends Presentation {


    private final static String TAG = "__presentation__";
    WeakReference<MediaPlayer> mMediaPlayerWeakReference = null;

    private PlayerPresentationLayoutBinding mBinding;

    private RenderViewBase mMVRenderView = new MVVideoRender(RenderData.getmMVRenderData());

    public PlayerPresentation(Context outerContext, Display display, MediaPlayer mediaPlayer) {
        super(outerContext, display);
        mMediaPlayerWeakReference = new WeakReference<>(mediaPlayer);
        this.getWindow().setGravity(Gravity.START | Gravity.TOP);
        this.getWindow().getAttributes().screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//9.0
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = PlayerPresentationLayoutBinding.inflate(LayoutInflater.from(this.getContext()));
        setContentView(mBinding.getRoot());

        initListener();
    }

    protected float sMatrix[] = new float[16];

    private void initListener() {

        mBinding.surfaceView.setSurfaceRenderListener(new ISurfaceRenderListener() {
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                Log.i(TAG, "onSurfaceCreated: ");
                mMVRenderView.onSurfaceCreated(PlayerPresentation.this.getContext());
            }

            @Override
            public void onSurfaceChanged(GL10 gl, int width, int height) {
                Log.i(TAG, "onSurfaceChanged: w:" + width + " height:" + height);
                mMVRenderView.onSurfaceChanged(width, height);

            }

            @Override
            public void onDrawFrameBegin(GL10 gl) {

                Log.i(TAG, "onDrawFrameBegin: run");
                mMVRenderView.draw(sMatrix);
            }

            @Override
            public void onDrawFrame(GL10 gl) {

            }

            @Override
            public void onDrawFrameEnd(GL10 gl) {

            }
        });

        if (mMVRenderView != null) {
            mMVRenderView.setVisiable(true);
        }

//        IMediaPlayer iMediaPlayer = mMediaPlayerWeakReference.get();
//        if (iMediaPlayer != null) {
//            iMediaPlayer.mvShow(true);
//        }
        updateFullScreen();
    }

    private void updateFullScreen() {
//        IMediaPlayer iMediaPlayer = mMediaPlayerWeakReference.get();
//
//        if (iMediaPlayer != null) {
//            FrameLayout.LayoutParams mvLayoutParams = iMediaPlayer.getMvLayoutParams();
//            mvLayoutParams.topMargin = 0;
//            mvLayoutParams.leftMargin = 0;
//            mvLayoutParams.rightMargin = 0;
//            mvLayoutParams.bottomMargin = 0;
//            mvLayoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
//            mvLayoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
//            mvLayoutParams.gravity = Gravity.CENTER;
//            iMediaPlayer.setMvLayoutParams(mvLayoutParams);
//        }
    }


}
